package net.example.lesson7;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author:zhaoxiaomin
 * @date:2018/10/24
 */
public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 200, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue <Runnable>(50));
        //执行饱和策略
        /*ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 200, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue <Runnable>(50),new ThreadPoolExecutor.CallerRunsPolicy());*/
        //允许核心线程超时
        //executor.allowCoreThreadTimeOut(true);
        /*ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 20, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(100));*/
        long s1 = System.currentTimeMillis();
        // 计数控制
        final CountDownLatch latch = new CountDownLatch(330);
        for(int i=0;i<330;i++){
            // 自定义执行体
            MyTask myTask = new MyTask(i,latch);

            executor.execute(myTask);
            System.out.println("线程池中线程数目："+executor.getPoolSize()+"，队列中等待执行的任务数目："+
                    executor.getQueue().size());
        }
        // 控制所有线程全部跑完（异步执行可以去掉）
        latch.await();
        long s2 = System.currentTimeMillis();
        long s =s2-s1;
        System.out.println("线程池执行时间:"+s);
        System.out.println("执行完毕=============================");
        executor.shutdown();
    }
}


class MyTask implements Runnable {
    private int taskNum;
    private CountDownLatch latch;

    public MyTask(int num,CountDownLatch latch) {
        this.taskNum = num;
        this.latch = latch;
    }

    @Override
    public void run() {
        System.out.println("正在执行task "+taskNum);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            latch.countDown();
        }
        System.out.println("task "+taskNum+"执行完毕");
    }
}